<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

use support\Request;
use support\Response;
use Webman\Route;
use Webman\Config;
use Illuminate\Container\Container;

// Webman Container
if (!Container::getInstance()) {
    $container = new Container;
    Container::setInstance($container);
}

if (!function_exists('request')) {
    /**
     * @return Request
     */
    function request()
    {
        return \support\Container::get(Request::class);
    }
}

if (!function_exists('response')) {
    /**
     * @param string $body
     * @param int $status
     * @param array $headers
     * @return Response
     */
    function response($body = '', $status = 200, $headers = [])
    {
        return new Response($status, $headers, $body);
    }
}

if (!function_exists('redirect')) {
    /**
     * @param string $location
     * @param int $status
     * @param array $headers
     * @return Response
     */
    function redirect(string $location, int $status = 302, array $headers = [])
    {
        $response = new Response($status, ['Location' => $location]);
        if (!empty($headers)) {
            $response->withHeaders($headers);
        }
        return $response;
    }
}

if (!function_exists('view')) {
    /**
     * @param string $template
     * @param array $vars
     * @param string|null $app
     * @return Response
     */
    function view(string $template, array $vars = [], string $app = null)
    {
        static $handler;
        if (null === $handler) {
            $handler = \support\Container::get(\Webman\View::class);
        }
        return new Response(200, [], $handler->render($template, $vars, $app));
    }
}

if (!function_exists('json')) {
    /**
     * @param mixed $data
     * @param int $options
     * @return Response
     */
    function json($data, $options = JSON_UNESCAPED_UNICODE)
    {
        return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
    }
}

if (!function_exists('session')) {
    /**
     * @param mixed $key
     * @param mixed $default
     * @return \Webman\Session\Session|mixed
     */
    function session($key = null, $default = null)
    {
        $session = request()->session();
        if (null === $key) {
            return $session;
        }
        if (is_array($key)) {
            $session->put($key);
            return null;
        }
        return $session->get($key, $default);
    }
}

if (!function_exists('config')) {
    /**
     * @param string|null $key
     * @param mixed|null $default
     * @return mixed
     */
    function config(string $key = null, $default = null)
    {
        return Config::get($key, $default);
    }
}

if (!function_exists('route')) {
    /**
     * @param string $name
     * @param array $parameters
     * @param bool $absolute
     * @return string
     */
    function route(string $name, array $parameters = [], bool $absolute = true)
    {
        return Route::url($name, $parameters, $absolute);
    }
}

if (!function_exists('url')) {
    /**
     * @param string $path
     * @param array $parameters
     * @return string
     */
    function url(string $path, array $parameters = [])
    {
        $request = request();
        if (!$request) {
            return $path;
        }
        if ($path[0] !== '/') {
            $path = '/' . $path;
        }
        $host = ($request->header('host') ?: '') . $path;
        if ($parameters) {
            $host .= '?' . http_build_query($parameters);
        }
        $scheme = $request->scheme();
        return "$scheme://$host";
    }
}


if (!function_exists('public_path')) {
    /**
     * @param string $path
     * @return string
     */
    function public_path(string $path = '')
    {
        return BASE_PATH . DIRECTORY_SEPARATOR . 'public' . ($path ? DIRECTORY_SEPARATOR . $path : '');
    }
}

if (!function_exists('app_path')) {
    /**
     * @param string $path
     * @return string
     */
    function app_path(string $path = '')
    {
        return BASE_PATH . DIRECTORY_SEPARATOR . 'app' . ($path ? DIRECTORY_SEPARATOR . $path : '');
    }
}

if (!function_exists('base_path')) {
    /**
     * Get the fully qualified path to the project root.
     *
     * @param string $path
     * @return string
     */
    function base_path(string $path = '')
    {
        if (!defined('BASE_PATH')) {
            define('BASE_PATH', dirname(__DIR__));
        }
        return BASE_PATH . ($path ? DIRECTORY_SEPARATOR . $path : '');
    }
}

if (!function_exists('config_path')) {
    /**
     * @param string $path
     * @return string
     */
    function config_path(string $path = '')
    {
        return BASE_PATH . DIRECTORY_SEPARATOR . 'config' . ($path ? DIRECTORY_SEPARATOR . $path : '');
    }
}

if (!function_exists('runtime_path')) {
    /**
     * @param string $path
     * @return string
     */
    function runtime_path(string $path = '')
    {
        return BASE_PATH . DIRECTORY_SEPARATOR . 'runtime' . ($path ? DIRECTORY_SEPARATOR . $path : '');
    }
}

spl_autoload_register(function ($class) {
    $prefix = 'app\\';
    if (strncmp($class, $prefix, 4) !== 0) {
        return;
    }
    $file = app_path() . str_replace('\\', '/', substr($class, 3)) . '.php';
    if (is_file($file)) {
        require_once $file;
    }
}); 